/*
    Copyright 2006-2011 Patrik Jonsson, sunrise@familjenjonsson.org

    This file is part of Sunrise.

    Sunrise is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Sunrise is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Sunrise.  If not, see <http://www.gnu.org/licenses/>.

*/

/** \file

    Implementations of the FITS-related emergence functions. These are
    kept in a separate file to avoid always having to include the
    CCfits headers. */

// $Id$

#ifndef __emergence_fits__
#define __emergence_fits__

#include "emergence.h"
#include "CCfits/CCfits"
#include <sstream> 
#include "blitz-fits.h"

/** Constructs an emergence object from (possibly a selection of) the
    CAMERAi-PARAMETER HDU's in a FITS file. */
template <typename T_image>
mcrx::emergence<T_image>::emergence (const CCfits::FITS& file,
				     const std::set<int>& load)
{
  int i = 0;
  try {
    while (true) {
      std::ostringstream ost;
      ost << "CAMERA" << i << "-PARAMETERS";
      // this will throw when we get to a camera that doesn't exist
      const CCfits::ExtHDU* hdu;
      try {
	hdu = &file.extension(ost.str() );
      }
      catch (CCfits::FITS::NoSuchHDU&) {
	const_cast<CCfits::FITS&> (file).read(string (ost.str()));
	hdu = &file.extension(ost.str() );
      }

      // see if this camera is selected
      if (!load.empty() && (load.find(i) == load.end())) {
	// cout << "skipping camera " << i << endl;
	++i;
	continue;
      }

      // read camera keywords (ideally, this would be done by a camera
      // constructor, but because they are const members and there is no
      // suitable CCfits function to return a keyword value, we have to
      // do it like this...)
      float cameradist;
      int xs, ys;

      CCfits::ExtHDU& silly = const_cast<CCfits::ExtHDU&> (*hdu);

      boost::shared_ptr<projection> proj = projection::read_projection(silly);

      silly.readKey("cameradist", cameradist);
      silly.readKey("ysize", ys);
      silly.readKey("xsize", xs );

      camera_numbers [i] = cameras.size(); 
      vec3d pos,dir,ny;
      silly.readKey("camposx",pos[0]);
      silly.readKey("camposy",pos[1]);
      silly.readKey("camposz",pos[2]);
      silly.readKey("camdirx", dir [0]);
      silly.readKey("camdiry", dir [1]);
      silly.readKey("camdirz", dir [2]);
      silly.readKey("camupx", ny [0]);
      silly.readKey("camupy", ny [1]);
      silly.readKey("camupz", ny [2]);
      cameras.push_back(new T_camera (pos, dir, ny, ys, proj));

      ++i;
    }
  }
  catch (CCfits::FITS::NoSuchHDU&) {} 
}  

/** Writes camera parameters to a FITS file. */
template <typename T_image>
void mcrx::emergence<T_image>::write_parameters (CCfits::FITS& file,
						 const T_unit_map& units) const
{
  // Loop over cameras and write parameters for each Note that it only
  // writes for cameras that are loaded, but it writes them with
  // correct numbers
  for (T_camera_map::const_iterator i = camera_numbers.begin();
       i != camera_numbers.end(); ++i) {
    std::ostringstream ost;
    ost << "CAMERA" << i->first << "-PARAMETERS";
    CCfits::Table* hdu = file.addTable(ost.str(), 0);
    cout << ost.str() << endl;
    cameras [i->second]->write_parameters(*hdu, units);
  }
}

#endif

